package com.iwomy.secureplat.platform.mapper;

import com.iwomy.secureplat.common.data.datascope.SecureplatBaseMapper;
import com.iwomy.secureplat.platform.entity.BasicDeviceEntity;
import com.iwomy.secureplat.platform.entity.BasicDeviceModelEntity;
import com.iwomy.secureplat.platform.entity.DeviceEventReportEntity;
import com.iwomy.secureplat.platform.vo.ProductRotationVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface BasicDeviceMapper extends SecureplatBaseMapper<BasicDeviceEntity> {


	@Select("SELECT tenant_id, dept_id, " +
			" SUM(CASE WHEN (device_state = 20 and `online` = 20) THEN 1 ELSE 0 END) AS normal," +
			" SUM(CASE WHEN (device_state = 30 or `online` = 10)  THEN 1 ELSE 0 END) AS fault," +
			" SUM(CASE WHEN (device_state = 40 and `online` = 20) THEN 1 ELSE 0 END) AS alarm," +
			" YEAR(CURRENT_TIMESTAMP) AS year," +
			" MONTH(CURRENT_TIMESTAMP) AS month," +
			" DAY(CURRENT_TIMESTAMP) AS day" +
			" FROM basic_device" +
			" WHERE del_flag='0'" +
			" GROUP BY tenant_id, dept_id;")
	List<DeviceEventReportEntity> deviceEventReport();

	@Select("<script> SELECT device_type_id,device_type_name, " +
			" count(*) AS total," +
			" SUM(CASE WHEN device_state = 30 THEN 1 ELSE 0 END) AS fault," +
			" SUM(CASE WHEN device_state = 40 THEN 1 ELSE 0 END) AS alarm," +
			" YEAR(CURRENT_TIMESTAMP) AS year," +
			" MONTH(CURRENT_TIMESTAMP) AS month," +
			" DAY(CURRENT_TIMESTAMP) AS day" +
			" FROM basic_device" +
			" WHERE del_flag='0'" +
			"	AND dept_id IN  " +
			"   <foreach item='item' index='index' collection='deptIds' open='(' separator=',' close=')'>" +
			"       #{item}" +
			"   </foreach>" +
			" GROUP BY device_type_id,device_type_name; </script>")
	List<ProductRotationVO> productRotation(@Param("deptIds") List<Long> deptIds);


	@Select("<script> SELECT DISTINCT(bdm.id) AS id,bdm.device_model_name " +
			" FROM basic_device as bd" +
			" LEFT JOIN basic_device_model as bdm ON bdm.id=bd.device_model_id " +
			" WHERE bd.del_flag='0' AND bdm.del_flag='0'  " +
			" AND bd.use_state=10 AND bdm.state=10	AND bd.dept_id IN  " +
			"   <foreach item='item' index='index' collection='deptIds' open='(' separator=',' close=')'>" +
			"       #{item}" +
			"   </foreach>" +
			" ; </script>")
	List<BasicDeviceModelEntity> homeGetModelListByDevice(@Param("deptIds") List<Long> deptIds);


	@Select("<script> SELECT device_type_id,device_model_id,device_type_name,device_model_name " +
			" FROM basic_device WHERE del_flag='0' AND dept_id IN " +
			"   <foreach item='item' index='index' collection='deptIds' open='(' separator=',' close=')'>" +
			"       #{item}" +
			"   </foreach>" +
			" GROUP BY device_type_id,device_model_id,device_type_name,device_model_name" +
			"</script> ")
	List<BasicDeviceEntity> allModel(@Param("tenantId")Long tenantId ,@Param("deptIds") List<Long> deptIds);


	Long getDeviceFaultCount(@Param("deviceIdList") List<Long> deviceIdList);

	Long getDeviceNormalCount(@Param("deviceIdList") List<Long> deviceIdList);

	Long getDeviceOnlineCount(@Param("deviceIdList") List<Long> deviceIdList);

	Long getDeviceOfflineCount(@Param("deviceIdList") List<Long> deviceIdList);

	Long getDeviceAlarmCount(@Param("deviceIdList") List<Long> deviceIdList);
}